package com.hskn.hss.module.carTrack.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.hskn.hss.core.annotation.SysLog;
import com.hskn.hss.core.enums.BusinessType;
import com.hskn.hss.entity.AjaxResult;
import com.hskn.hss.exception.HssException;
import com.hskn.hss.module.button.entity.SysButton;
import com.hskn.hss.module.carTrack.dto.CarTrackDto;
import com.hskn.hss.module.carTrack.entity.CarTrack;
import com.hskn.hss.module.carTrack.mapper.CarTrackMapper;
import com.hskn.hss.module.carTrack.service.CarTrackService;
import com.hskn.hss.module.carTrack.to.AlarmAndViolationCarTo;
import com.hskn.hss.module.carTrack.to.LastCarTrackTo;
import com.hskn.hss.module.carTrack.to.CleanListTo;
import com.hskn.hss.module.carTrack.to.MileageAndOilWearTo;
import com.hskn.hss.module.carTrack.vo.AlarmAndViolationCar;
import com.hskn.hss.module.carTrack.vo.FaclCleanListVo;
import com.hskn.hss.module.carTrack.vo.LastCarTrackVo;
import com.hskn.hss.module.carTrackOriginal.to.CarTrackGridTo;
import com.hskn.hss.module.common.to.OilNumTO;
import com.hskn.hss.module.tlfacilitietype.mapper.TlFacilitiesTypeMapper;
import com.hskn.hss.task.HssTask;
import com.hskn.hss.task.WorkTask;
import com.hskn.hss.utils.Tools;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;


/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author hskn
 * @since 2021-05-21
 */
@Api(tags = {"-car_track"}, description = "车辆轨迹")
@Slf4j
@RestController
@RequestMapping("/hss/carTrack")
public class CarTrackController {

    @Autowired
    private CarTrackService carTrackService;
    @Resource
    private CarTrackMapper carTrackMapper;
    @Autowired
    private HssTask hssTask;
    @Autowired
    private WorkTask workTask;

    @RequestMapping(value = "taskInit", method = RequestMethod.GET)
    public void taskInit() {
        hssTask.updateTimeConsuming();
    }

    @RequestMapping(value = "trashPickup", method = RequestMethod.GET)
    public void employeeWorkPlan() {
        workTask.trashPickup();
    }

    @RequestMapping(value = "initCarAttendance", method = RequestMethod.GET)
    public void updateCarAddr() {
        hssTask.initCarAttendance();
    }


    @RequestMapping(value = "list", method = RequestMethod.POST)
    public AjaxResult<Object> list(HttpServletRequest request) {
        try {
            JSONObject params = JSON.parseObject(Tools.getBody(request));
            List json = carTrackService.mList(params);
            return AjaxResult.success(json);
        } catch (HssException e) {
            return AjaxResult.fail(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("操作失败");
        }
    }

    @RequestMapping(value = "selectPage", method = RequestMethod.POST)
    public AjaxResult<Object> selectPage(HttpServletRequest request) {
        try {
            JSONObject params = JSON.parseObject(Tools.getBody(request));
            JSONObject json = carTrackService.selectPage(params);
            return AjaxResult.success(json);
        } catch (HssException e) {
            return AjaxResult.fail(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("操作失败");
        }
    }

    @RequestMapping(value = "getCarTrackGrid", method = RequestMethod.POST)
    public AjaxResult<Object> getCarTrackGrid(@RequestBody @Validated CarTrackGridTo carTrackGridTo) {
        try {
            return AjaxResult.success(carTrackService.getCarTrackGrid(carTrackGridTo));
        } catch (Exception e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("操作失败");
        }
    }

    @RequestMapping(value = "getLiveLjqyCar", method = RequestMethod.POST)
    public AjaxResult<Object> getLiveLjqyCar(HttpServletRequest request) {
        try {
            JSONObject params = JSON.parseObject(Tools.getBody(request));
            List list = carTrackService.getLiveLjqyCar(params);
            return AjaxResult.success(list);
        } catch (HssException e) {
            return AjaxResult.fail(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("操作失败");
        }
    }


    /***
     * 新增信息
     * @param request
     * @return
     */
    @SysLog(businessType = BusinessType.INSERT, tableName = "car_track", bean = CarTrack.class, beanMapper = CarTrackMapper.class, names = "carId")
    @RequestMapping(value = "insert", method = RequestMethod.POST)
    public AjaxResult<Object> insert(HttpServletRequest request) {
        JSONObject params = JSON.parseObject(Tools.getBody(request));
        return carTrackService.insert(params);
    }

    @RequestMapping(value = "violationsAndAlarms", method = RequestMethod.POST)
    public AjaxResult<Object> violationsAndAlarms(HttpServletRequest request) {
        try {
            JSONObject params = JSON.parseObject(Tools.getBody(request));
            List json = carTrackService.violationsAndAlarms(params);
            return AjaxResult.success(json);
        } catch (HssException e) {
            return AjaxResult.fail(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("操作失败");
        }
    }

    @RequestMapping(value = "cleanPoint", method = RequestMethod.POST)
    public AjaxResult<Object> cleanPoint() {
        try {
            return AjaxResult.success(carTrackService.cleanPoint());
        } catch (Exception e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("操作失败");
        }
    }

    @RequestMapping(value = "mileageAndOilWear", method = RequestMethod.POST)
    public AjaxResult<Object> mileageAndOilWear(@RequestBody @Validated MileageAndOilWearTo mileageAndOilWearTo) {
        try {
            return AjaxResult.success(carTrackService.mileageAndOilWear(mileageAndOilWearTo));
        } catch (HssException e) {
            return AjaxResult.fail(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("操作失败");
        }
    }

    @ApiOperation(value = "车辆轨迹和违规和报警接口", notes = "车辆轨迹")
    @ApiResponses({
            @ApiResponse(code = 200, message = "ok", response = AlarmAndViolationCar.class)
    })
    @PostMapping("alarmAndViolationCarList")
    public AjaxResult<Object> alarmAndViolationCarList(@RequestBody @Validated AlarmAndViolationCarTo alarmAndViolationCarTo) {
        try {
            return AjaxResult.success(carTrackService.alarmAndViolationCarList(alarmAndViolationCarTo));
        } catch (HssException e) {
            return AjaxResult.fail(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("操作失败");
        }
    }

    @ApiOperation(value = "车辆详情速度油量查询接口", notes = "速度油量")
    @ApiResponses({
            @ApiResponse(code = 200, message = "ok", response = LastCarTrackVo.class)
    })
    @PostMapping("lastCarTrack")
    public AjaxResult<Object> lastCarTrack(HttpServletRequest request, @RequestBody @Validated LastCarTrackTo lastCarTrackTo) {
        try {
            CarTrackDto lastCarTrackDto = carTrackService.lastCarTrack(new CarTrackDto(lastCarTrackTo));
            return AjaxResult.success(Tools.getBean(JSONObject.toJSONString(lastCarTrackDto.getCarTrack()), LastCarTrackVo.class));
        } catch (HssException e) {
            return AjaxResult.fail(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("操作失败");
        }
    }

    @ApiOperation(value = "垃圾桶已清未清查询接口", notes = "已清未清")
    @ApiResponses({
            @ApiResponse(code = 200, message = "ok", response = FaclCleanListVo.class)
    })
    @PostMapping("cleanList")
    public AjaxResult<Object> cleanList(@RequestBody @Validated CleanListTo cleanListTo) {
        try {
            return AjaxResult.success(carTrackService.cleanList(cleanListTo));
        } catch (HssException e) {
            return AjaxResult.fail(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("操作失败");
        }
    }

    @SysLog(businessType = BusinessType.UPDATE, tableName = "car_track", idKey = "id", bean = CarTrack.class, beanMapper = CarTrackMapper.class, names = "carId")
    @RequestMapping(value = "updateOilAndTimeConsuming", method = RequestMethod.POST)
    public AjaxResult<Object> updateOilAndTimeConsuming(@RequestBody @Validated MileageAndOilWearTo mileageAndOilWearTo) {
        try {
            carTrackService.updateOilAndTimeConsuming(mileageAndOilWearTo);
            return AjaxResult.success("操作成功");
        } catch (Exception e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("操作失败");
        }
    }

    //    @SysLog(businessType = BusinessType.UPDATE, idKey = "id", beanMapper = CarTrackMapper.class, names = "carId")
    @RequestMapping(value = "updateRptCarWorkByWorkType", method = RequestMethod.POST)
    public AjaxResult<Object> updateRptCarWorkByWorkType(HttpServletRequest request) {
        try {
            carTrackService.updateRptCarWorkByWorkType();
            return AjaxResult.success("操作成功");
        } catch (Exception e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("操作失败");
        }
    }

    @RequestMapping(value = "insertRptCarWork", method = RequestMethod.POST)
    public AjaxResult<Object> insertRptCarWork(@RequestBody @Validated MileageAndOilWearTo mileageAndOilWearTo) {
        try {
//            carTrackService.insertRptCarWork(mileageAndOilWearTo);
            return AjaxResult.success("操作成功");
        } catch (Exception e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("操作失败");
        }
    }

    /**
     * 处理停滞点
     *
     * @param oilNumTO
     * @return
     */
    @PostMapping(value = "deleteRealOffLineTrack")
    public AjaxResult deleteRealOffLineTrack(@RequestBody @Validated OilNumTO oilNumTO) {
        try {
            carTrackService.processingStopPointCarTrack(oilNumTO);
            return AjaxResult.success("操作成功");
        } catch (Exception e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("操作失败");
        }
    }
    /**
     * 从KfkCarLocation补点到CarTrack
     *
     * @param oilNumTO
     * @return
     */
    @PostMapping(value = "supplementaryCarTrackPoints")
    public AjaxResult supplementaryCarTrackPoints(@RequestBody @Validated OilNumTO oilNumTO) {
        try {
            carTrackService.supplementaryCarTrackPoints(oilNumTO);
            return AjaxResult.success("操作成功");
        } catch (Exception e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("操作失败");
        }
    }
}

